package love.contact.adapter;

import java.io.File;
import java.util.Arrays;

import love.contact.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import wwk.common.util.FileSorter;

public class FileListAdapter extends BaseAdapter{

	private String path;
	private LayoutInflater mInflater;
	private File[] fileArray;
	
	public FileListAdapter(Context context,String path)
	{
		this.path=path;
		mInflater = LayoutInflater.from(context);
		refresh();
	}
	
	public void refresh()
	{
		if(path!=null)
		{
			File file=new File(path);
			fileArray=file.listFiles();
			if(fileArray!=null)
			{
				Arrays.sort(fileArray, new FileSorter(FileSorter.TYPE_MODIFIED_DATE_DOWN));
			}
		}
	}
	
	@Override
	public int getCount() 
	{
		if(fileArray!=null)
		{
			return fileArray.length;
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 if (convertView == null) 
        {
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
        }
        
        File file=fileArray[position];
        String fileName=file.getName();
        
        TextView textView=(TextView)convertView;
        textView.setText(fileName);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(18);
        
        Drawable drawable=mInflater.getContext().getResources().getDrawable(R.drawable.ic_file_dark);
        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        textView.setCompoundDrawables(drawable,null,null,null);
        textView.setCompoundDrawablePadding(10);

        return convertView;
	}

	public File getFile(int index)
	{
		File file=null;
		if(fileArray!=null && fileArray.length>index)
		{
			file=fileArray[index];
		}
		return file;
	}
}
